﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TFMAPI;


namespace TwitterFollowersMonitor
{
    public partial class AddTwitterUserDlg : Form
    {
        //Dialog result
        public TwitterUserInfo resTUI = null;
        
        public AddTwitterUserDlg()
        {
            InitializeComponent();
        }

        private void cmdVerify_Click(object sender, EventArgs e)
        {
            string nameToCheck = txtName.Text.Trim();
            if (nameToCheck == "")
            {
                txtName.Focus();
                return;
            }

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                resTUI = TwitterAPIWrapper.GetTwitterUserInfo(nameToCheck);
                userInfo.DisplayUserInfo(resTUI);
                userInfo.Visible = true;
                cmdOK.Enabled = true;
            }
            catch (InexistentTwitterUserException)
            {
                lblDontExists.Visible = true;
            }
            catch (QuotaExceededException qeEx)
            {
                MessageBox.Show(string.Format("The maximun quota of Twitter API calls ({0} per hour) has been reached. You now will have to wait until {1}", qeEx.MaxCallsPerHour, qeEx.NextAvailableTime.ToLongTimeString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Unexpected exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally 
            {
                Cursor.Current = Cursors.Default;
                txtName.SelectAll();
                txtName.Focus();
            }

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            resTUI = null;
            cmdOK.Enabled = false;
            userInfo.Visible = false;
            lblDontExists.Visible = false;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}
